package com.greenold.service.service.impl;

import com.greenold.service.domain.dto.NurseDto;
import com.greenold.service.domain.vo.NurseNameVO;
import com.greenold.service.domain.vo.RoomBedVO;
import com.greenold.service.domain.vo.RoomVO;
import com.greenold.service.mapper.RoomBedMapper;
import com.greenold.service.service.IRoomBedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author YueYang
 * Created on 2025/10/15 14:24
 * @version 1.0
 */
@Service
public class RoomBedServiceImpl implements IRoomBedService {

    @Autowired
    private RoomBedMapper roomBedMapper;

    /**
     * 查询当前楼层的所有房间列表
     *
     * @param floorId 楼层id
     * @return 返回响应集合
     */
    @Override
    public List<RoomVO> queryList(Long floorId) {
        List<RoomVO> roomVOList = roomBedMapper.queryList(floorId);
        //根据楼层id 查询所有的床位护理员id


        return roomVOList;
    }

    /**
     * 查询所有楼层id
     *
     * @return 返回楼层id对应的集合
     */
    @Override
    public List<Long> queryIdList() {
        return roomBedMapper.queryIdList();
    }

    /**
     * 获取所有的护理员信息，用于前端展示
     *
     * @return 返回所有的护理员信息
     */
    @Override
    public List<NurseNameVO> queryNurses() {
        return roomBedMapper.queryNurses();
    }

    /**
     * 更新护理员绑定关系
     *
     * @param nurseDto 包含床位ID列表和护理员ID列表的数据传输对象
     * @return 返回影响的行数
     */
    @Override
    @Transactional
    public int updateNurseBinding(NurseDto nurseDto) {
        // 1. 先删除现有的绑定关系
        int deleteCount = roomBedMapper.deleteNurseBindingByBedIds(nurseDto.getBedIdList());
        
        // 2. 如果有护理员需要绑定，则插入新的绑定关系
        int insertCount = 0;
        if (nurseDto.getNurseIdList() != null && !nurseDto.getNurseIdList().isEmpty()) {
            insertCount = roomBedMapper.insertNurseBinding(nurseDto.getBedIdList(), nurseDto.getNurseIdList());
        }
        
        return deleteCount + insertCount;
    }
}
